<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
    	$this->login();
    }
    public function login(){

    	//供应商商家后台登录
    	//判断shopid 是否存在
    	///if(!empty($_SESSION['shopid'])){
      if(!empty($_SESSION['supperid'])){
    		$this->redirect('Goods/index');
    	}else{
    		$this->display('login');
    	}
    	
    }
    //验证用户名和密码
    public function verifyUser(){
    	// 检测验证码
		    $code=I('post.vercode','','strip_tags');

		    $data['name']=I('post.username','','strip_tags');
		    $data['pass']=md5(I('post.password','','strip_tags'));
		    $data['_logic']='AND';
		    $verify = new \Think\Verify();
		    $result['vercode']=$verify->check($code);	

		    if(empty($result['vercode']))
        {    
               echo "<script type='text/javascript'>alert('验证码错误请重新填写'); window.history.go(-1);window.location.reload();</script>";
		          
        }
        
		    //验证用户名是否存在
		    $result['password']= M('supper_user')->where("name='%s'",$data['name'])->getField('pass');
		    //var_dump($result['password']);die;
        if(empty($result['password']))
        {
                echo "<script type='text/javascript'>alert('用户名不存在'); window.history.go(-1);window.location.reload();</script>";
		    }else
        {
               $result['supperid']=M('supper_user')->where($data)->getField('supperid');

               if(empty($result['supperid']))
               {
                    
                    echo "<script type='text/javascript'>alert('密码错误'); window.history.go(-1);window.location.reload();</script>";
               }else
               {
               	 $_SESSION['name']=$data['name'];
               	 $_SESSION['supperid']=$result['supperid'];
               	// R('Adminlog/addlog',  array(array('登录','后台')));
                  $_SESSION['is_Login']=1;
               	$this->redirect('Item/index');
               }
		    }

    }
    public function logout(){

    	if(!empty($_SESSION['supperid'])){
            
			$_SESSION=array();
			$this->login();
		}else{
			$this->login();
		}
    }
    //生成验证码
	public function vercode(){
		$Verify = new \Think\Verify();
		$Verify->fontSize =20;
		$Verify->length   = 4;
		$Verify->useNoise = false;
        $Verify->entry();
	}

	//空操作
	public function _empty(){
		$this->login();
	}
}